import { Trim } from '@/utils/util';
export default {
	data() {
		return {
            modal:false,
            modalTitle:"",
            parentResource:[],
            originParentResource:[],
            parentName:'',
            resource:[],
            resour:{
                "id":null,
                "parent":null,
                "type":"menu",
                "name":"",
                "remark":"",
                "path":"",
                "status":"ENABLE"
            },
            
		}
	},
	computed: {
		
	},
	watch:{
		
    },
    
	methods: {
        open(item){       
            this.$http.post('/user/resource/find',{"parent":item.id})
                .then(res => {
                    if(res.data.code=='601'){
                        item.show = true;
                        for(let i of res.data.data){
                            i.show = false;
                        }
                        for(let i=0;i<this.resource.length;i++){
                            if(item.id==this.resource[i].id){
                                this.resource.splice(i+1,0,...res.data.data);
                                break;
                            }
                            
                        }
                    }
                })
        },
        close(item){    
            const id = item.id;
            for(let i=0;i<this.resource.length;i++){
                if(this.resource[i].parent==id){                   
                    this.resource.splice(i,1);
                    i--
                }
            }       
            item.show = false;
        },
        //确定修改或者添加
		modalOk(){    
            this.resour.name = Trim(this.resour.name);
            let url = "/user/resource/update";
            if(this.modalTitle=="添加资源"){
                url = "/user/resource/create";
            }
            this.$http.post(url,this.resour)
                .then(res => {
                    if(res.data.code=='601'){
                        this.$Message.success(`${this.modalTitle}成功！`);
						this.getResource();
						return;
					}
					this.$Notice.error({
	                    title: `${this.modalTitle}失败！`,
	                    desc: res.data.message
					});			
                })
        },
        //点击添加按钮
        add(){
            this.getParentResource();
            this.parentName = "";
            this.resour = {
                "id":null,
                "parent":null,
                "type":"MENU",
                "name":"",
                "remark":"",
                "path":"",
                "status":"ENABLE"
            },
            this.modalTitle = "添加资源";
            this.modal = true;
        },
        //点击编辑按钮
        async edit(resour){
            
            this.modalTitle = "编辑资源";
            this.modal = true;
            this.resour = JSON.parse(JSON.stringify(resour));
            
            await this.getParentResource();
            const id = resour.id;
            const parentId = resour.parent;
            for(let i=0;i<this.originParentResource.length;i++){
                if(this.originParentResource[i].id == parentId){                   
                    this.parentName = this.originParentResource[i].name;       
                    break;
                }
            }
            //编辑的上级资源不能选择当前资源
            for(let i=0;i<this.originParentResource.length;i++){
                if(this.originParentResource[i].id == id){                   
                    this.originParentResource.splice(i,1);                       
                    this.parentResource.splice(i,1);                       
                    break;
                }
            }

        },
        //删除
        dele(id){           
            this.$Modal.confirm({
                title: '删除',
                content: '<p>确认删除该条资源？</p>',
                loading: true,
                onOk: () => {
                    this.$http.post('/user/resource/delete',{"id":id})
                        .then((res) => {
                            if(res.data.code=='601'){
                                this.$Modal.remove();
                                this.$Message.success(`删除成功！`);
                                this.getResource();
                                return;
                            }
                            this.$Notice.error({
                                title: `删除失败！`,
                                desc: res.data.message
                            });	
                        })                    
                }
            });
        },
        //自动完成框的search
        parentSearch(value){
            this.parentResource = [];
            for(let i of this.originParentResource){
                if(i.name.includes(value)){                   
                    this.parentResource.push(i);
                }
            }
        },
        //选择上级资源
        chooseParentOption(value){           
            for(let i of this.parentResource){               
                if(value==i.name){
                    this.resour.parent = i.id;                   
                    break;
                }
            }
        },
        //获取上级资源
        getParentResource(){
            return this.$http.post('/user/resource/list/menu')
                .then(res => {
                    if(res.data.code=='601'){
                        this.originParentResource = JSON.parse(JSON.stringify(res.data.data));
                        this.parentResource = JSON.parse(JSON.stringify(res.data.data));
                    }
                    return true;
                })
        },
        //资源列表
        getResource(){
            this.$http.post('/user/resource/list')
                .then(res => {
                    if(res.data.code=='601'){
                        for(let i of res.data.data.list){
                            i.show = false;
                        }
                        
                        this.resource = res.data.data.list;
                        this.getParentResource();
                        return;
                    }
                    this.$Notice.error({
	                    title: '查询失败',
	                    desc: res.data.message
					});
                })
                .catch((err) => {
					//请求失败的处理
					this.$Notice.error({
	                    title: '查询失败',
	                    desc: err
	                });
				})
        }
	},
	components:{
		
	},
	created() {
        this.getResource();       
	}
}